// !! 侧边菜单栏
import { defineStore } from 'pinia'
import { getRole } from '@u/index'
import { cloneDeep } from 'lodash'
const permissionStore = defineStore('permission', {
  state: () => {
    return {
       // 存储 当前 所有的 导航目录菜单
      navs: [
        {
          label: '仪表盘',
          icon: 'HomeFilled',
          path: '/dashBoard',
          isMenu: false, // 代表是否是目录
          roles: '*' // 所有角色都可以显示
        },
        {
          label: '商品管理',
          icon: 'GoodsFilled',
          isMenu: true,
          roles: ['admin', 'superAdmin'],
          children: [
            {
              icon: 'MoreFilled',
              path: '/itemLists',
              label: '商品列表',
              roles: ['admin', 'superAdmin']
            },
            {
              icon: 'Grid',
              path: '/cateLists',
              label: '商品分类',
              roles: ['admin', 'superAdmin']
            },
          ]
        },
        {
          label: '设置',
          icon: 'Tools',
          isMenu: true,
          roles: ['superAdmin'],
          children: [
            {
              icon: 'UserFilled',
              path: '/UserInfo',
              label: '个人中心',
              roles: [ 'superAdmin']
            },
          ]
        }
      ]
    }
  },
  getters: {
    auth_menus (state) {
      // 对于 navs进行过滤
      // 获取用户role
      const navs = cloneDeep(state.navs)
      const role:any = getRole()
      const authMenus: any[] = []
      navs.forEach(menu => {
        if (menu.roles === '*' || menu.roles.includes(role)) {
          // 根据判断是否是 目录 进行二次循环
          if (menu.isMenu) {
            // 继续过滤children
            menu.children.filter(subMenu => subMenu.roles === '*' || subMenu.roles.includes(role))
          }
          authMenus.push(menu)
        }
      })
      return authMenus
    }
  }
})
export default permissionStore